package com.wang.controller.api;

import com.wang.util.SmsUtil;
import com.wang.utils.ConstantUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import com.wang.utils.VerifyCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Classname ApiSmsController
 * @Description TODO
 * @Date 2025/3/21 11:49
 * @Created by pc
 */
@RestController
@RequestMapping("/api/sms")
@Api(value = "发送短信",tags = "ApiSmsController")
public class ApiSmsController {

    @Resource
    private SmsUtil smsUtil;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/sendSms")
    @ApiOperation(value = "发送验证码",notes = "前台用户注册发送短信验证码")
    @ApiImplicitParam(value = "用户电话",name = "phone",required = true,dataType = "String")
    public ResponseUtil sendVerifyCode(String phone){
        String code = VerifyCodeUtil.getCode();
        // smsUtil.sendVerifyCode(phone,code);
        System.out.println(phone +"-" + code);
        // 最好的方案是 我们生成一个UUID 并且返回给前端 用户提交的时候 拿着这个UUID 去redis中查询
        // 找到之后 比对用户输入的code 和 phone
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        stringRedisTemplate.opsForValue().set(uuid,phone+"-"+code);
        stringRedisTemplate.expire(uuid, ConstantUtil.VERIFY_CODE_TIMEOUT, TimeUnit.MINUTES );
        Map<String,Object> map = new HashMap<>();
        map.put("uuid",uuid);
        return ResponseUtil.get(ResponseEnum.SMS_VERIFY_4_CODE_SUCCESS,map);
    }
}
